package cn.cs.service.impl;

import cn.cs.client.RestFaultTypeClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.AppOrderConcelcause;
import cn.cs.pojo.FaultType;
import cn.cs.pojo.ServiceType;
import cn.cs.service.FaultTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class FaultTypeServiceImpl implements FaultTypeService {

    @Resource
    private RestFaultTypeClient faultTypeClient;

    @Override
    public List<AppOrderConcelcause> getConcelcause() throws Exception {
        return faultTypeClient.getConcelcause();
    }

    @Override
    public List<ServiceType> getFaultCategory() throws Exception {
        return faultTypeClient.getFaultCategory();
    }

    @Override
    public List<FaultType> getTypeTree(){
        return faultTypeClient.getTypeTree();
    }

    @Override
    public List<FaultType> getFaultType(){
        return faultTypeClient.getFaultType();
    }

    @Override
    public List<FaultType> getFaultTypeTree(){
        return faultTypeClient.getFaultTypeTree();
    }

    @Override
    public List<Map<String,Object>> getFaultTypeByPid(String id){
        return faultTypeClient.getFaultTypeByPid(id);
    }


    @Override
    public List<FaultType> getTwoFaultCategory(String level) throws Exception {
        return faultTypeClient.getTwoFaultCategory(level);
    }

    @Override
    public List<Map<String,Object>> getFaultTypeById(String parentId)throws Exception{
            //获取第一层级信息
            List<Map<String,Object>> subList = faultTypeClient.getFaultTypeByParentId(parentId);
            //获取内层信息
            for(int i=0;i<subList.size();i++){
                Set set = subList.get(i).entrySet();
                Iterator it = set.iterator();
                while(it.hasNext()){
                    Map.Entry entry = (Map.Entry) it.next();
                    if("id".equals(entry.getKey())){
                        List<Map<String,Object>> secList = faultTypeClient.getFaultTypeByParentId(entry.getValue().toString());
                        subList.get(i).put("subList",secList);
                        break;
                    }
                }
            }
            return subList;
    }

    @Override
    public List<Map<String, Object>> getFaultTypeByParentId(String parentId) throws Exception {
        return faultTypeClient.getFaultTypeByParentId(parentId);
    }

    @Override
    public List<FaultType> getFaultTypeListByMap(Map<String,Object> param)throws Exception{
        return faultTypeClient.getFaultTypeListByMap(param);
    }
    @Override
    public Integer getFaultTypeCountByMap(Map<String,Object> param)throws Exception{
        return faultTypeClient.getFaultTypeCountByMap(param);
    }

    public Integer addFaultType(FaultType faultType)throws Exception{
            return faultTypeClient.addFaultType(faultType);
    }

    public Integer modifyFaultType(FaultType faultType)throws Exception{
        return faultTypeClient.modifyFaultType(faultType);
    }
    @Override
    public Integer deleteFaultTypeById(String id)throws Exception{
        return faultTypeClient.deleteFaultTypeById(id);
    }

    @Override
    public Integer deleteServiceTypeById(String id)throws Exception{
        return faultTypeClient.deleteServiceTypeById(id);
    }
    @Override
    public Page<FaultType> queryFaultTypePageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = faultTypeClient.getFaultTypeCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<FaultType> faultTypeList = faultTypeClient.getFaultTypeListByMap(param);
        page.setRows(faultTypeList);
        return page;
    }


    @Override
    public Page<ServiceType> queryServiceTypePageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = faultTypeClient.getServiceTypeCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ServiceType> faultTypeList = faultTypeClient.getServiceTypeListByMap(param);
        page.setRows(faultTypeList);
        return page;
    }


    public Integer addServiceType(ServiceType serviceType)throws Exception{
        return faultTypeClient.addServiceType(serviceType);
    }

    public Integer modifyServiceType(ServiceType serviceType)throws Exception{
        return faultTypeClient.modifyServiceType(serviceType);
    }

    @Override
    public FaultType getFaultTypeByIdOnlyOne(String id)throws Exception{
        return faultTypeClient.getFaultTypeByIdOnlyOne(id);
    }


}
